package com.lenovo.smarttraffic.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Chuangyi_Fragment extends Fragment {
    private Spinner spinner1;
    private Button button1;
    private ListView listview1;
    private List<String[]> shuju_list;
    private MyAdapter myAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = getLayoutInflater().inflate(R.layout.activity_chuangyi__fragment,null);
        spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        button1 = (Button) view.findViewById(R.id.button1);
        listview1 = (ListView) view.findViewById(R.id.listview1);
        monishuju();
        range(1,0);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (spinner1.getSelectedItemPosition()){
                    case 0:range(1,0);break;
                    case 1:range(0,0);break;
                    case 2:range(1,1);break;
                    case 3:range(0,1);break;
                    case 4:range(1,2);break;
                    case 5:range(0,2);break;
                    case 6:range(1,3);break;
                    case 7:range(0,3);break;
                }
            }
        });
        return view;
    }
    private void range(int method,int weizhi){
        if (method==1){
            for (int i=0;i<shuju_list.size();i++){
                for (int j=0;j<shuju_list.size()-1-i;j++){
                    if (Integer.valueOf(shuju_list.get(j)[weizhi]) > Integer.valueOf(shuju_list.get(j+1)[weizhi])){
                        String[] temp = shuju_list.get(j);
                        shuju_list.set(j,shuju_list.get(j+1));
                        shuju_list.set(j+1,temp);
                    }
                }
            }
        }else {
            for (int i=0;i<shuju_list.size();i++){
                for (int j=0;j<shuju_list.size()-1-i;j++){
                    if (Integer.valueOf(shuju_list.get(j)[weizhi]) < Integer.valueOf(shuju_list.get(j+1)[weizhi])){
                        String[] temp = shuju_list.get(j);
                        shuju_list.set(j,shuju_list.get(j+1));
                        shuju_list.set(j+1,temp);
                    }
                }
            }
        }
        Toast.makeText(getContext(), "已按照您的要求排序！", Toast.LENGTH_SHORT).show();
        myAdapter.notifyDataSetChanged();

    }
    private void monishuju(){
        Random random = new Random();

        shuju_list = new ArrayList<>();
        shuju_list.add(new String[]{
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100))
        });
        shuju_list.add(new String[]{
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100))
        });
        shuju_list.add(new String[]{
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100))
        });
        shuju_list.add(new String[]{
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100)),
                String.valueOf(random.nextInt(100))
        });
        myAdapter = new MyAdapter();
        View head_view = getLayoutInflater().inflate(R.layout.chuanyi_list_view_item,null);
        listview1.addHeaderView(head_view);
        listview1.setAdapter(myAdapter);
    }
    class MyAdapter extends BaseAdapter{
        private TextView lukou;
        private TextView hongdeng;
        private TextView huangdeng;
        private TextView lvdeng;
        @Override
        public int getCount() {
            return shuju_list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View view = getLayoutInflater().inflate(R.layout.chuanyi_list_view_item,null);


            lukou = (TextView) view.findViewById(R.id.lukou);
            hongdeng = (TextView) view.findViewById(R.id.hongdeng);
            huangdeng = (TextView) view.findViewById(R.id.huangdeng);
            lvdeng = (TextView) view.findViewById(R.id.lvdeng);
                lukou.setText(shuju_list.get(position)[0]);
                hongdeng.setText(shuju_list.get(position)[1]);
                huangdeng.setText(shuju_list.get(position)[2]);
                lvdeng.setText(shuju_list.get(position)[3]);

            return view;
        }
    }
}
